package com.avic.rong.adapter.show;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.bean.show_bean.WenDaBean;
import com.avic.rong.questionOrAnswer.activity.QuestionDetailsActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 关注
 * Creater: Created by wangyz on 2017/1/10.
 * Email: 936804097@qq.com
 */

public class WenDaAdapter extends RecyclerView.Adapter<WenDaAdapter.RecommendViewHolder> {


    private static final int STYPE_NO_PICTURE = 0;
    private static final int STYPE_ONE_PICTURE = 1;
    private static final int STYPE_THREE_PICTURE = 3;

    private Context mContext;
    private List<WenDaBean> data;

    public WenDaAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<WenDaBean> list) {
        this.data = list;
        notifyDataSetChanged();
    }

    @Override
    public WenDaAdapter.RecommendViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.view_command, parent, false);
        if (viewType == STYPE_ONE_PICTURE) {
            view = LayoutInflater.from(mContext).inflate(R.layout.view_command_one_pic, parent, false);
        } else if (viewType == STYPE_THREE_PICTURE) {
            view = LayoutInflater.from(mContext).inflate(R.layout.view_command_three_pic, parent, false);
        }

        return new RecommendViewHolder(view);
    }

    public static String longToDate(long lo) {
        Date date = new Date(lo);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sd.format(date);
    }

    @Override
    public void onBindViewHolder(WenDaAdapter.RecommendViewHolder holder, int position) {


        String logo = data.get(position).getImgs();
        if (logo != null && !logo.equals("")) {
            String[] split = logo.split(";");
            WenDaBean wenDaBean = data.get(position);
            if (split.length == 3) {
                long publicTime = wenDaBean.getPublicTime();
                String timedate = longToDate(publicTime);
                holder.three_ribao.setVisibility(View.GONE);
                holder.three_before.setText(timedate.split(" ")[0]);
                holder.three_readsize.setText("回答" + wenDaBean.getAnsNum() + "");
                String title = wenDaBean.getTitle().replaceAll("<br>", "/n");
                holder.three_tv_category_name.setText(title);
                Glide.with(mContext).load(split[0]).error(R.mipmap.error)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.three_oneimage);
                Glide.with(mContext).load(split[1]).error(R.mipmap.error)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.three_twoimage);
                Glide.with(mContext).load(split[2]).error(R.mipmap.error)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.three_threeimage);
            } else if (split.length > 0) {
                long publicTime = wenDaBean.getPublicTime();
                String timedate = longToDate(publicTime);
                holder.one_ridu.setVisibility(View.GONE);
                holder.one_before.setText(timedate.split(" ")[0]);
                holder.one_readsize.setText("回答" + wenDaBean.getAnsNum() + "");
                String title = wenDaBean.getTitle().replaceAll("<br>", "/n");
                holder.one_tv_category_name.setText(title);
                Glide.with(mContext).load(split[0])
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).error(R.mipmap.error).into(holder.one_oneimage);
            }
        } else {
            WenDaBean wenDaBean = data.get(position);
            long publicTime = wenDaBean.getPublicTime();
            String timedate = longToDate(publicTime);
            holder.no_ribao.setVisibility(View.GONE);
            holder.no_before.setText(timedate.split(" ")[0]);
            holder.no_readsize.setText("回答" + wenDaBean.getAnsNum() + "");
            String title = wenDaBean.getTitle().replaceAll("<br>", "/n");
            holder.no_tv_category_name.setText(title);

        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WenDaBean wenDaBean = data.get(position);
                int queId = wenDaBean.getQueId();
                mContext.startActivity(QuestionDetailsActivity.queryIntent(mContext, queId + ""));
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        String logo = data.get(position).getImgs();
        if (!TextUtils.isEmpty(logo)) {
            String[] split = logo.split(";");
            if (split.length == 3) {
                return STYPE_THREE_PICTURE;
            } else if (split.length > 0) {
                return STYPE_ONE_PICTURE;
            } else {
                return STYPE_NO_PICTURE;
            }
        } else {
            return STYPE_NO_PICTURE;
        }

    }

    @Override
    public int getItemCount() {
        if (data != null) {
            return data.size();
        }
        return 0;
    }


    class RecommendViewHolder extends RecyclerView.ViewHolder {

        TextView tv_category_name;
        ImageView one_oneimage, three_twoimage, three_oneimage, three_threeimage;
        TextView no_before, no_ribao, no_readsize, no_tv_category_name, one_before, one_tv_category_name, one_toutiao, one_ridu, one_readsize,
                three_before, three_readsize, three_ribao, three_toutiao, three_tv_category_name;

        public RecommendViewHolder(View itemView) {
            super(itemView);

            tv_category_name = itemView.findViewById(R.id.tv_category_name);
            no_before = itemView.findViewById(R.id.no_before);
            no_ribao = itemView.findViewById(R.id.no_ribao);
            no_readsize = itemView.findViewById(R.id.no_readsize);
            no_tv_category_name = itemView.findViewById(R.id.no_tv_category_name);

            one_before = itemView.findViewById(R.id.one_before);
            one_oneimage = itemView.findViewById(R.id.one_oneimage);
            one_readsize = itemView.findViewById(R.id.one_readsize);
            one_ridu = itemView.findViewById(R.id.one_ribao);
            //     one_toutiao = itemView.findViewById(R.id.one_toutiao);
            one_tv_category_name = itemView.findViewById(R.id.one_tv_category_name);

            three_before = itemView.findViewById(R.id.three_before);
            three_oneimage = itemView.findViewById(R.id.three_oneimage);
            three_twoimage = itemView.findViewById(R.id.three_twoimage);
            three_threeimage = itemView.findViewById(R.id.three_threeimage);
            three_readsize = itemView.findViewById(R.id.three_readsize);
            three_ribao = itemView.findViewById(R.id.three_ribao);
            //    three_toutiao = itemView.findViewById(R.id.three_toutiao);
            three_tv_category_name = itemView.findViewById(R.id.three_tv_category_name);

        }
    }
}
